package com.qs.eggs.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.qs.eggs.Constants;
import com.qs.eggs.R;
import com.qs.eggs.account.LoginActivity;
import com.qs.eggs.api.HttpApi;
import com.qs.eggs.refresh.utils.TDevice;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;


public class PoiAdapter extends RecyclerView.Adapter<PoiAdapter.ViewHolder>  implements View.OnClickListener{
    private LayoutInflater mInflater;
    private List<Map<String, Object>> imagelist=new ArrayList<>();
    private Context context;

    public PoiAdapter(Context context, List<Map<String, Object>> imagelist) {
        mInflater = LayoutInflater.from(context);
        this.imagelist = imagelist;
        this.context=context;
    }
    private OnItemClickListener mOnItemClickListener = null;

    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View arg0) {
            super(arg0);
        }

        ImageView imageView,cart;
        TextView name,jiage;
    }

    @Override
    public int getItemCount()
    {
        return imagelist.size();
    }

    /**
     * 创建ViewHolder
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = mInflater.inflate(R.layout.tehui,
                viewGroup, false);
        int width = (int) TDevice.getScreenWidth();
        int width1=width/3;
        ViewGroup.LayoutParams para = view.getLayoutParams();
        para.width = width1;
        view.setLayoutParams(para);
        ViewHolder viewHolder = new ViewHolder(view);
        view.setOnClickListener(this);
        viewHolder.imageView = (ImageView) view.findViewById(R.id.imageView);
        viewHolder.name = (TextView) view.findViewById(R.id.name);
        viewHolder.jiage = (TextView) view.findViewById(R.id.jiage);
        viewHolder.cart = (ImageView) view.findViewById(R.id.cart);
        return viewHolder;
    }

    /**
     * 设置值
     */
    @Override
    public void onBindViewHolder(ViewHolder viewHolder,final int position) {
        viewHolder.itemView.setTag(position);
        Picasso.with(context).load(Constants.URL_BASE+"productimages"+"/"+imagelist.get(position).get("autoname"))
                .into(viewHolder.imageView);
        Picasso.with(context).load(R.mipmap.icon_cart)
                .into(viewHolder.cart);
        viewHolder.name.setText(imagelist.get(position).get("proname").toString());
        viewHolder.jiage.setText("¥"+imagelist.get(position).get("saleprice")+"");
        viewHolder.cart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addCart(imagelist.get(position).get("proid").toString(),imagelist.get(position).get("proname").toString(),imagelist.get(position).get("saleprice").toString(),
                        imagelist.get(position).get("type").toString(),imagelist.get(position).get("specification").toString());
            }
        });
    }
    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

//添加购物车
    private void addCart(final String proid, final String proname, final String price, final String type, final String specification){
        HttpApi.cheakIsLogin(new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                if(s.equals("\"false\"")){
                    LoginActivity.show(context);
                }else {
                    HashMap<String,String> map=new HashMap<String, String>();
                    map.put("proid",proid);
                    map.put("proname",proname);
                    map.put("price",price);
                    map.put("count","1");
                    map.put("type",type);
                    map.put("specification",specification);
                    Gson gson=new Gson();
                    String str=gson.toJson(map);
                    OkGo.post(Constants.URL_BASE+"shoppingcart?action=addShoppingCart")
                            .params("data",str)
                            .execute(new StringCallback() {
                                @Override
                                public void onSuccess(String s, Call call, Response response) {
                                    if (s.equals("\"true\"")){
                                        Toast.makeText(context,"加入购物车成功",Toast.LENGTH_SHORT).show();
                                    }else{
                                        Toast.makeText(context,"加入购物车失败",Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });

                }
            }
        });
    }
}
